package vn.vfriends.soffice.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author chaungoctuan@gmail.com
 */
@Entity
@Table(name="PERMISSION")
@NamedQueries({
    @NamedQuery(name = "Permission.findByMenu", query = "SELECT p FROM Permission p WHERE p.menu = :menu")})
public class Permission implements Serializable {

    public enum Code {
        USER_VIEW, USER_CREATE, USER_EDIT, USER_DELETE
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Integer id;
    
    @Size(max=50)
    @Column(length=50, nullable=false, unique=true)
    private String name;
    
    @Column(length=20, nullable=false, unique=true)
    @Enumerated(EnumType.STRING)
    @NotNull
    private Code code;
    
    @Size(max=50)
    @Column(length=50)
    private String url;
    
    @ManyToOne
    @JoinColumn(name = "MENU_ID")
    @NotNull
    private Menu menu;
    
    @Size(max = 200)
    @Column(length = 200)
    private String description;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Code getCode() {
        return code;
    }

    public void setCode(Code code) {
        this.code = code;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }
    
    @Override
    public int hashCode() {
        int hash = 5;
        hash = 59 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Permission other = (Permission) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
    
    @Override
    public String toString() {
        return "Quyền[" + name + ']';
    }
}
